from django.db import models
from django.contrib.auth.models import User
from PMSP.utils.base_model import BaseModel

# 用户收货地址模型类
class Address(BaseModel):

    # related_name,通过用户查询自己所有的地址时候，可以使用user.addresses
    user = models.ForeignKey(User,on_delete=models.CASCADE,related_name="addresses",verbose_name="地址所属的用户")
    title = models.CharField(max_length=20,verbose_name="收货人")
    receiver = models.CharField(max_length=20,verbose_name="收货人",default="") # 冗余字段
    # 通过area.province_addresses可以知道哪些地址引用了该省
    province = models.ForeignKey('areas.Area',on_delete=models.PROTECT,related_name="province_addresses",verbose_name="地址所属的省")
    city = models.ForeignKey('areas.Area',on_delete=models.PROTECT,related_name="city_addresses",verbose_name="地址所属的市")
    district = models.ForeignKey('areas.Area',on_delete=models.PROTECT,related_name="district_addresses",verbose_name="地址所属的区")
    place = models.CharField(max_length=50,verbose_name="详细地址")
    mobile = models.CharField(max_length=11,verbose_name="手机号")
    tel = models.CharField(max_length=20,verbose_name="固定电话",null=True,blank=True,default="")
    email = models.CharField(max_length=30,verbose_name="邮箱",null=True,blank=True,default="")
    is_delete = models.BooleanField(default=False,verbose_name="逻辑删除")

    class Meta:
        db_table = "tb_address"
        # 默认排序方式
        ordering = ["-update_time"]


